﻿namespace eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms
{
    using Microsoft.WindowsCE.Forms;
    using eeenno.SpeeedReaderUI.NativeWebBrowser.Win32;
    using System;
    using System.Drawing;
    using System.Reflection;
    using System.Windows.Forms;

    public abstract class ControlEx : Control, IWin32Window
    {
        private System.Windows.Forms.BorderStyle m_border;
        private IntPtr m_childhwnd = IntPtr.Zero;
        private IntPtr m_hwnd = IntPtr.Zero;
        private static IntPtr m_instance = Core.GetModuleHandle(null);
        private ControlMessageWindow m_msgwnd;
        private string m_name = string.Empty;
        private bool m_native;
        private bool m_recreating;
        private object m_tag;

        protected ControlEx(bool native)
        {
            this.m_native = native;
            if (native)
            {
                this.m_msgwnd = new ControlMessageWindow(this);
            }
        }

        public void CreateControl()
        {
            if (this.m_native)
            {
                if (this.m_childhwnd != IntPtr.Zero)
                {
                    this.m_recreating = true;
                    Win32Window.DestroyWindow(this.m_childhwnd);
                    this.m_childhwnd = IntPtr.Zero;
                }
                eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms.CreateParams createParams = this.CreateParams;
                if (createParams.ClassName != null)
                {
                    this.m_childhwnd = Win32Window.CreateWindowEx(createParams.ExStyle, createParams.ClassName, createParams.Caption, createParams.ClassStyle, createParams.X, createParams.Y, createParams.Width, createParams.Height, createParams.Parent, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
                }
                this.m_recreating = false;
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (this.m_childhwnd != IntPtr.Zero)
            {
                Win32Window.DestroyWindow(this.m_childhwnd);
                this.m_childhwnd = IntPtr.Zero;
            }
            if (this.m_msgwnd != null)
            {
                this.m_msgwnd.Dispose();
            }
            base.Dispose(disposing);
        }

        public static Control GetControlByName(Control parent, string name)
        {
            FieldInfo field = parent.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);
            if (field == null)
            {
                return null;
            }
            object obj2 = field.GetValue(parent);
            if (obj2 == null)
            {
                return null;
            }
            return (Control) obj2;
        }

        public static IntPtr GetHandle(Control c)
        {
            c.Capture = true;
            IntPtr capture = Win32Window.GetCapture();
            c.Capture = false;
            return capture;
        }
        
        public static string GetName(Control control)
        {
            foreach (FieldInfo info in control.Parent.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase))
            {
                if (info.GetValue(control.Parent).Equals(control))
                {
                    return info.Name;
                }
            }
            return string.Empty;
        }

        internal virtual void OnEraseBkgndMessage(ref Microsoft.WindowsCE.Forms.Message m)
        {
        }

        protected override void OnGotFocus(EventArgs e)
        {
            if (this.m_native)
            {
                if ((this.ChildHandle != IntPtr.Zero) && (Win32Window.GetFocus() != this.ChildHandle))
                {
                    Win32Window.SetFocus(this.ChildHandle);
                }
            }
            else
            {
                base.OnGotFocus(e);
            }
        }

        internal void OnMessage(ref Microsoft.WindowsCE.Forms.Message m)
        {
            this.OnNotifyMessage(ref m);
        }

        protected virtual void OnNotifyMessage(ref Microsoft.WindowsCE.Forms.Message m)
        {
        }

        protected override void OnParentChanged(EventArgs e)
        {
            this.m_hwnd = IntPtr.Zero;
            this.CreateControl();
            base.OnParentChanged(e);
        }

        protected override void OnResize(EventArgs e)
        {
            if (this.m_native)
            {
                Win32Window.SetWindowPos(this.m_msgwnd.Hwnd, HWND.TOP, 0, 0, base.Width, base.Height, 0);
                if (this.ChildHandle != IntPtr.Zero)
                {
                    Win32Window.SetWindowPos(this.ChildHandle, HWND.TOP, 0, 0, base.Width, base.Height, 0);
                }
            }
            base.OnResize(e);
        }

        public void SetBounds(int x, int y, int width, int height)
        {
            base.Bounds = new Rectangle(x, y, width, height);
        }

        public System.Windows.Forms.BorderStyle BorderStyle
        {
            get
            {
                return this.m_border;
            }
            set
            {
                this.m_border = value;
                if (value == null)
                {
                    Win32Window.UpdateWindowStyle(this.Handle, 0x800000, 0);
                }
                else
                {
                    Win32Window.UpdateWindowStyle(this.Handle, 0, 0x800000);
                }
            }
        }

        protected internal IntPtr ChildHandle
        {
            get
            {
                return this.m_childhwnd;
            }
            set
            {
                this.m_childhwnd = value;
            }
        }

        public bool Created
        {
            get
            {
                return (this.m_childhwnd != IntPtr.Zero);
            }
        }

        protected virtual eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms.CreateParams CreateParams
        {
            get
            {
                eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms.CreateParams param = new eeenno.SpeeedReaderUI.NativeWebBrowser.Windows.Forms.CreateParams();
                param.Parent = this.m_msgwnd.Hwnd;
                param.ClassStyle = 0x50010000;
                param.Width = base.Width;
                param.Height = base.Height;
                param.X = 0;
                param.Y = 0;
                return param;
            }
        }

        public IntPtr Handle
        {
            get
            {
                if (this.m_hwnd == IntPtr.Zero)
                {
                    base.Capture = true;
                    this.m_hwnd = Win32Window.GetCapture();
                    base.Capture = false;
                }
                return this.m_hwnd;
            }
        }

        internal IntPtr HostHandle
        {
            get
            {
                return this.m_msgwnd.Hwnd;
            }
        }

        protected static IntPtr Instance
        {
            get
            {
                return m_instance;
            }
        }

        public string Name
        {
            get
            {
                if (this.m_name == string.Empty)
                {
                    this.m_name = GetName(this);
                }
                return this.m_name;
            }
        }

        public bool RecreatingHandle
        {
            get
            {
                return this.m_recreating;
            }
        }

        public object Tag
        {
            get
            {
                return this.m_tag;
            }
            set
            {
                this.m_tag = value;
            }
        }

        internal class ControlMessageWindow : Microsoft.WindowsCE.Forms.MessageWindow
        {
            private ControlEx m_parent;
            private const int WM_ERASEBKGND = 20;
            private const int WM_NOTIFY = 0x4e;

            internal ControlMessageWindow(ControlEx parent)
            {
                this.m_parent = parent;
                Win32Window.SetParent(base.Hwnd, this.m_parent.Handle);
                Win32Window.SetWindowLong(base.Hwnd, -16, 0x46000000);
                Win32Window.SetWindowLong(base.Hwnd, -20, 8);
                Win32Window.SetWindowPos(base.Hwnd, HWND.TOP, 0, 0, this.m_parent.Width, this.m_parent.Height, SWP.SHOWWINDOW | SWP.NOZORDER | SWP.NOSIZE);
            }

            protected override void WndProc(ref Microsoft.WindowsCE.Forms.Message m)
            {
                switch (m.Msg)
                {
                    case 20:
                        this.m_parent.OnEraseBkgndMessage(ref m);
                        break;

                    case 0x4e:
                        this.m_parent.OnMessage(ref m);
                        break;

                    case 0x111:
                        this.m_parent.OnMessage(ref m);
                        break;
                }
                if (m.Result != new IntPtr(-1))
                {
                    base.WndProc(ref m);
                }
            }
        }
    }
}

